﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NRemedy;
using NRemedy.Linq;
using System.Configuration;
using Oracle.DataAccess.Client;

namespace Syncer
{
    class Program
    {
        //dictionary stored company and its id
        static Dictionary<string, int> companyDic = new Dictionary<string, int>();
        //dictionary stored businessSystem and company mapped to its id
        //incident.BusinessSystem + incident.Company
        static Dictionary<string, int> bsDic = new Dictionary<string, int>();

       internal static void Main(string[] args)
        {
            try
            {
                Log.OpenLog();
                if (args == null)
                    return;
                if (args.Length == 0)
                    Usage();

                //incident
                //change
                //problem
                //...
                string application = args[0];
                DateTime starttime = Convert.ToDateTime(args[1]);
                //DateTime endtime = Convert.ToDateTime(args[2]);
                DateTime endtime = DateTime.Now;

                InitDic();
                Log.Info("Init company and business OK.");
                Log.Info("Company Count : " + companyDic.Count);
                Log.Info("Business Count : " + bsDic.Count);



                switch (application.ToLower())
                {

                    case "incident":
                        new IncidentImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    case "sla":
                        new SLAImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    case "change":
                        new ChangeImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    case "problem":
                        new ProblemImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    case "escalation":
                        new EscalationImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    case "all":
                        new ChangeImport().Execute(
                          new Param
                          {
                              StartTime = starttime,
                              EndTime = endtime,
                              ComDic = companyDic,
                              BsDic = bsDic
                          }
                          );
                        new IncidentImport().Execute(
                           new Param
                           {
                               StartTime = starttime,
                               EndTime = endtime,
                               ComDic = companyDic,
                               BsDic = bsDic
                           }
                           );
                        new SLAImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        new ProblemImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        new EscalationImport().Execute(
                            new Param
                            {
                                StartTime = starttime,
                                EndTime = endtime,
                                ComDic = companyDic,
                                BsDic = bsDic
                            }
                            );
                        break;
                    default:
                        break;
                }
                Log.Info("Import End.");
            }
            finally
            {
                Log.CloseLog();

            }

            //Console.Read();
        }

        private static void InitDic()
        {
            using (OracleDataAccess oda = new OracleDataAccess())
            {
                //init businessSystem dic
                string sql = @"select b.id as id,b.name as bname,s.name as cname from BUSINESSSYSTEM b inner join sitebranch s on b.siteid = s.id";
                OracleDataReader reader = null;
                try
                {
                    reader = oda.ExecuteReader(sql);
                    while (reader.Read())
                    {
                        bsDic.Add(reader["bname"].ToString() + reader["cname"].ToString(), Convert.ToInt32(reader["id"]));
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception("Init businessSystem failed.", ex);
                    throw ex;
                }
                finally
                {
                    reader.Close();
                }

                //init company dic
                sql = @"select id,name from sitebranch";
                try
                {
                    reader = oda.ExecuteReader(sql);
                    while (reader.Read())
                    {
                        companyDic.Add(reader["name"].ToString(), Convert.ToInt32(reader["id"]));
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception("Init site failed.", ex);
                    throw ex;
                }
                finally
                {
                    reader.Close();
                }
            }

        }

        private static void Usage()
        {
            Console.WriteLine("argument error!");

        }
    }
}
